package com.yjc.hire.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.ibm.icu.impl.locale.XCldrStub;
import com.yjc.hire.exceptions.GraceException;
import com.yjc.hire.exceptions.MyCustomException;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.server.Properties.BaseIndustryProperties;
import com.yjc.hire.server.VO.TopIndustryWithThirdListVO;
import com.yjc.hire.server.mapper.IndustryCustomMapper;
import com.yjc.hire.server.mapper.IndustryMapper;
import com.yjc.hire.server.service.IndustryService;
import com.yjc.pojo.Industry;
import io.seata.common.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/25
 * </p>
 */

@Service
@Slf4j
public class IndustryServiceImpl extends BaseIndustryProperties implements IndustryService {

    @Autowired
    private IndustryMapper industryMapper;
    @Autowired
    private IndustryCustomMapper industryCustomMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据名称判断industry是否存在
     * @param industryName
     * @return
     */
    @Override
    public boolean getIndustryExistByName(String industryName) {
        Industry industry = industryMapper.selectindustryByName(industryName);
        return industry != null;
    }

    @Override
    public void createIndustryNode(Industry industry) {
        industryMapper.insert(industry);
    }

    @Override
    public List<Industry> getIndusryTopNodeList() {
        List<Industry> topIndustryList = null;
        String topIndustryListStr = stringRedisTemplate.opsForValue().get(TOP_INDUSTRY_LIST);
        if (StringUtils.isNoneBlank(topIndustryListStr)) {
            topIndustryList = JSON.parseArray(topIndustryListStr, Industry.class);
        }else {
            topIndustryList = industryMapper.getIndustryTopNodeList();
            stringRedisTemplate.opsForValue().set(TOP_INDUSTRY_LIST, JSON.toJSONString(topIndustryList));
        }
        return topIndustryList;
    }

    @Override
    public List<Industry> getChildrenNodeList(String industryId) {
        return industryMapper.getChildrenNodeList(industryId);
    }

    @Override
    @Transactional
    public void updateNode(Industry industry) {
        industryMapper.updateById(industry);
    }

    @Override
    public void deleteIndustryNode(String industryId) {
        Industry industry = getIndustryById(industryId);
        if (industry.getLevel() == 1 || industry.getLevel() == 2) {
            Long count = getChildNodeCount(industryId);
            if (count > 0) {
                throw new MyCustomException(ResponseStatusEnum.FAILED);
            }resetRedisIndustry(industry);
            industryMapper.deleteById(industryId);
        }
    }

    @Override
    public Long getChildNodeCount(String industryId) {
        return industryMapper.getChildNodeCount(industryId);
    }
    public Industry getIndustryById(String industryId) {
        return industryMapper.getIndustryById(industryId);
    }

    @Override
    public List<Industry> getThirdList(String topIndustryId) {
        List<Industry> thirdIndustryList = null;
        String thirdIndustryStr = stringRedisTemplate.opsForValue().get(THIRD_INDUSTRY_LIST +
                BY_TOP_ID + topIndustryId);
        if (StringUtils.isNoneBlank(thirdIndustryStr)) {
            thirdIndustryList = JSON.parseArray(thirdIndustryStr, Industry.class);
        }
        else {
            Map<String, Object> parMap = new HashMap<>();
            parMap.put("topIndustryId", topIndustryId);
            thirdIndustryList = industryCustomMapper.getThirdListByTop(parMap);
            if (!CollectionUtils.isEmpty(thirdIndustryList)) {
                stringRedisTemplate.opsForValue().set(THIRD_INDUSTRY_LIST + BY_TOP_ID + topIndustryId, JSON.toJSONString(thirdIndustryList));
            }else{
                stringRedisTemplate.opsForValue().set(THIRD_INDUSTRY_LIST + BY_TOP_ID + topIndustryId
                ,"[]",10 * 60);
            }
        }
        return thirdIndustryList;
    }

    public void resetRedisIndustry(Industry industry) {
        if (industry.getLevel() == 1){
            stringRedisTemplate.delete(TOP_INDUSTRY_LIST);
            List<Industry> topIndustryList = getIndusryTopNodeList();
            stringRedisTemplate.opsForValue().set(TOP_INDUSTRY_LIST, JSON.toJSONString(topIndustryList));

            try {
                Thread.sleep(300);
                stringRedisTemplate.delete(TOP_INDUSTRY_LIST);
            }catch (InterruptedException e){
                List<Industry> topDouIndustryList = getIndusryTopNodeList();
                stringRedisTemplate.opsForValue().set(TOP_INDUSTRY_LIST, JSON.toJSONString(topDouIndustryList));
            }
        }
        else if (industry.getLevel() == 3){
            String topIndustryId = getTopIndustryId(industry.getId());
            stringRedisTemplate.delete(THIRD_INDUSTRY_LIST + BY_TOP_ID + topIndustryId);
            List<Industry> thirdIndustryList = getThirdList(topIndustryId);
            stringRedisTemplate.opsForValue().set(THIRD_INDUSTRY_LIST + BY_TOP_ID
            + topIndustryId, JSON.toJSONString(thirdIndustryList));
        }
    }

    public String getTopIndustryId(String thirdIndustryId) {
        return industryCustomMapper.getTopIndustryId(thirdIndustryId);
    }

    @Override
    public List<TopIndustryWithThirdListVO> getAllThirdIndustryList() {
        return industryCustomMapper.getAllThirdIndustryList();
    }
}
